package com.manli.api.service;

import com.manli.api.pojo.QueryParamsMajorType;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.MajorTypeDao;
import com.manli.api.dto.MajorType;
import org.springframework.transaction.annotation.Transactional;


@Service("majorTypeService")
public class MajorTypeService {
    @Autowired
    MajorTypeDao majorTypeDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParamsMajorType params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from major_type where status=1 ");
        sql.append(StringUtils.isEmpty(params.getMajorName())?"":" and major_name like '%" + params.getMajorName() + "%'");
        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<MajorType> pageQuery = new PageQuery<MajorType>(params.getPage(),params.getLimit());
        PageQuery<MajorType> query =sqlManager.execute(sqlReady,MajorType.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<MajorType> list = majorTypeDao.execute("select * from major_type where status=1 ");
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer majorTypeId) {
        MajorType majorType = majorTypeDao.single(majorTypeId);
        return ResultUtil.success(majorType);
    }

    @Transactional
    public Result save(MajorType majorType) {
        if(StringUtils.isEmpty(majorType.getMajorName())){
            return ResultUtil.error(-1,"主营类别名称为空");
        }
        List<MajorType> list = majorTypeDao.execute("select * from major_type where major_name = ?",majorType.getMajorName());
        if(list.size() > 0){
            return ResultUtil.error(-1,"主营类别名称已存在");
        }

        majorTypeDao.insert(majorType);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(MajorType majorType) {
        if(majorType.getMajorTypeId() == null){
            return ResultUtil.error(-1,"主营类别id为空");
        }
        if(StringUtils.isEmpty(majorType.getMajorName())){
            return ResultUtil.error(-1,"主营类别名称为空");
        }

        List<MajorType> list = majorTypeDao.execute("select * from major_type where major_name = ? and major_type_id != ?",majorType.getMajorName(),majorType.getMajorTypeId());
        if(list.size() > 0){
            return ResultUtil.error(-1,"主营类别名称已存在");
        }

        majorTypeDao.updateTemplateById(majorType);
        return ResultUtil.success();
    }
    @Transactional
	public Result updateTemplateById(MajorType majorType) {
        majorTypeDao.updateTemplateById(majorType);
        return ResultUtil.success();
    }
    @Transactional
    public Result removeById(Integer majorTypeId) {
        majorTypeDao.executeUpdate("update major_type set status = 0 where major_type_id = ?",majorTypeId);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            majorTypeDao.executeUpdate("update major_type set status = 0 where major_type_id = ?",id);
        }
        return ResultUtil.success();
    }



}
